<?php
/* @var $model_word array */

/* @var $model_pdf array */

use backend\models\file\AttExtend;
use backend\utils\HtmlUtil;
use yii\helpers\Html;
use yii\helpers\Url;

?>
<style>
    .file_container {
        text-align: left;
    }

</style>
<div class="row">
    <div class="col-md-12">
        <div class="panel-body">
            <table class="table table-striped table-bordered">
                <tbody>

                <tr data-type="word">
                    <td rowspan="2" style="width: 30%">审计报告</td>
                    <td>word</td>
                    <td>
                        <input type="file" class="input_file">
                        <div class="file_container">
                            <?= HtmlUtil::html_a_down($model_word) ?>
                        </div>
                    </td>
                    <td>
                        <?php
                        echo Html::a('上传', NULL, [
                            "class" => "btn btn-primary btn_save",
                        ]);
                        ?>
                    </td>
                </tr>

                <tr data-type="pdf">
                    <td>pdf</td>
                    <td>
                        <input type="file" class="input_file">
                        <div class="file_container">
                            <?= HtmlUtil::html_a_down($model_pdf) ?>
                        </div>
                    </td>
                    <td>
                        <?php
                        echo Html::a('上传', NULL, [
                            "class" => "btn btn-primary btn_save",
                        ]);
                        ?>
                    </td>
                </tr>

                </tbody>
            </table>

        </div>
    </div>
</div>
<?php
$js = <<<JS

JS;
$this->registerJs($js);
?>
<script>
    $(".btn_save").click(function () {
        var tr = $(this).closest("tr");
        var formData = new FormData();
        formData.append("file", tr.find(".input_file")[0].files[0]);
        formData.append("file_type", tr.attr('data-type'));
        $.ajax({
            url: '<?= Url::to(['up-file-alert-save']) ?>', /*接口域名地址*/
            type: 'post',
            data: formData,
            contentType: false,
            processData: false,
            success: function (res) {
                alert(res.msg)
                if (res.code == 1) {
                    var a_element = "<a class='' href='" + res.data.downloadUrl + "'>" + res.data.file_name + "</a>";
                    tr.find(".file_container").html(a_element)
                }
            }
        })
    })

</script>
